package com.spreedairy.portal.service.persistence;

import com.liferay.portal.kernel.dao.orm.QueryPos;
import com.liferay.portal.kernel.dao.orm.SQLQuery;
import com.liferay.portal.kernel.dao.orm.Session;
import com.liferay.portal.kernel.dao.orm.Type;
import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
import com.liferay.util.portlet.PortletProps;
import com.spreedairy.portal.model.Recommendation;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class RecommendationFinderImpl extends BasePersistenceImpl<Recommendation> implements RecommendationFinder{
	public List<Long> getTopRecommendationIds(long companyId, long groupId){
		List<Long> result = new ArrayList<Long>();
		Session session = null;
		try {

			session = openSession();
			SQLQuery q = session.createSQLQuery(PortletProps.get("top.recommendation.query"));

			q.addScalar("itemId", Type.LONG);
			q.addScalar("score", Type.LONG);

			QueryPos qPos = QueryPos.getInstance(q);
			qPos.add(companyId);
			qPos.add(groupId);
			
			Iterator<Object[]> itr = q.list().iterator();

			while (itr.hasNext()) {
				Object[] array = itr.next();
				long itemId = (Long) array[0];
				result.add(itemId);
			}
			return result;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			closeSession(session);
		}
		return result;
	}
	
	public List<Long> getTopWhishlistIds(long companyId, long groupId){
		List<Long> result = new ArrayList<Long>();
		Session session = null;
		try {

			session = openSession();
			SQLQuery q = session.createSQLQuery(PortletProps.get("top.wishlist.query"));

			q.addScalar("itemId", Type.LONG);
			q.addScalar("score", Type.LONG);

			QueryPos qPos = QueryPos.getInstance(q);
			qPos.add(companyId);
			qPos.add(groupId);
			
			Iterator<Object[]> itr = q.list().iterator();

			while (itr.hasNext()) {
				Object[] array = itr.next();
				long itemId = (Long) array[0];
				result.add(itemId);
			}
			return result;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			closeSession(session);
		}
		return result;
	}

}
